package com.meicunbing.viberplugin;

import org.json.JSONException;
import org.json.JSONObject;

import com.meicunbing.viberplugin.com.Weather;
import com.meicunbing.viberplugin.db.DatabaseHelper;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
	private EditText phoneNumEditText;

	private EditText messageEditText;

	private Button sendButton;

	private Button weatherButton;

	private Button viewCityCodeButton;

	private String DB_VER_NAME = "DB_VR";
	private int DB_VER_NUM = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// 载入配置文件
		SharedPreferences sp = getSharedPreferences(DB_VER_NAME, 0);
		int db_ver_num = sp.getInt("DB_VER_NUM", 0);
		Log.i("db_ver", "默认版本" + DB_VER_NUM, null);
		Log.i("db_ver", "缓存版本" + db_ver_num, null);
		if (db_ver_num < DB_VER_NUM) {
			DatabaseHelper dbh = new DatabaseHelper(this);
			SQLiteDatabase sqliteDatabase = dbh.getWritableDatabase();
			dbh.onUpgrade(sqliteDatabase, DB_VER_NUM, db_ver_num);
			Log.i("db_upgrade", "更新数据库", null);
			Editor editor = sp.edit();
			editor.putInt("DB_VER_NUM", DB_VER_NUM);
			editor.commit();
			Log.i("db_ver", "更新数据库版本号", null);
		}
		// Boolean silient=settings.getBoolean("silientMode",false);
		phoneNumEditText = (EditText) findViewById(R.id.phoneNumEditText);
		messageEditText = (EditText) findViewById(R.id.messageEditText);
		sendButton = (Button) findViewById(R.id.sendButton);
		weatherButton = (Button) findViewById(R.id.weatherButton);
		viewCityCodeButton = (Button) findViewById(R.id.viewCityCodeButton);
		phoneNumEditText.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (phoneNumEditText.getText().toString()
						.equals(getString(R.string.phone_notice))) {
					phoneNumEditText.setText("");
				}

			}

		});
		messageEditText.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (messageEditText.getText().toString()
						.equals(getString(R.string.message_notice))) {
					messageEditText.setText("");
				}

			}

		});
		sendButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				String num = phoneNumEditText.getText().toString();
				if (num.equals(getString(R.string.phone_notice)) || num == null
						|| num.equals("".toString())) {
					Toast.makeText(MainActivity.this, R.string.phone_notice,
							Toast.LENGTH_LONG).show();
					phoneNumEditText.setText("");
					phoneNumEditText.requestFocus();
					return;
				}
				String message = messageEditText.getText().toString();
				if (message.equals(getString(R.string.message_notice))
						|| message.equals("".toString()) || message == null) {
					Toast.makeText(MainActivity.this, R.string.message_notice,
							Toast.LENGTH_LONG).show();
					messageEditText.setText("");
					messageEditText.requestFocus();
					return;
				}
				Toast.makeText(MainActivity.this, "向" + num + "发送：" + message,
						Toast.LENGTH_LONG).show();
				Uri uri = Uri.parse("smsto:" + num);
				Intent it = new Intent(Intent.ACTION_DEFAULT, uri);
				it.putExtra("sms_body", message);
				startActivity(it);
			}
			// com.viber.voip.action.MESSAGES

		});

		weatherButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				new Thread() {
					public void run() {
						Looper.prepare();
						Weather w = new Weather();
						w.setCityCode("101190101");
						JSONObject wo = w.getfWeather();
						if (wo == null) {
							MainActivity.this.showWeather("暂无天气预报".toString());
							return;
						}
						try {
							MainActivity.this.showWeather(wo.getString("city")
									+ wo.getString("date_y")
									+ wo.getString("week") + ":"
									+ wo.getString("temp1")
									+ wo.getString("weather1")
									+ wo.getString("fx1")
									+ wo.getString("wind1")
									+ wo.getString("index")
									+ wo.getString("index_d"));
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						Looper.loop();
					}
				}.start();
			}

		});

		viewCityCodeButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// setContentView(R.layout.activity_citylist);
				Intent intent = new Intent();
				intent.setClass(MainActivity.this, CityListActivity.class);// 前面一个是一个Activity后面一个是要跳转的Activity
				startActivity(intent);// 开始界面的跳转函数
				// MainActivity.this.finish();//关闭显示的Activity
			}

		});
	}

	public void showWeather(String s) {
		Toast.makeText(MainActivity.this, s, Toast.LENGTH_LONG).show();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

}
